package com.android.project.wilfred;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.Menu.Item;
import android.widget.Button;

public class projectWilfred extends Activity {
    /** Called when the activity is first created. */
	
	//PREFERENCES DECLARATIONS		
	private String PREFS_NAME;
	private String PREFS_THEME;
	@SuppressWarnings("unused")
	private String PREFS_PUBLICMAP;
	
	//ACTIVITY REQUEST CODES
	private static final int ACTIVITY_OPTIONS = 1337;
	
	//HANDLERS
	private final Handler mHandler = new Handler();
	
	//MENU ITEMS
	private static final int RESUMEGAME = Menu.FIRST;
	private static final int OPTIONS = Menu.FIRST + 1;
	private static final int LISTSCRIPTS = Menu.FIRST + 2;
	
	//-------------------------------------------------------+
	// START PROJECT WILFRED
	//-------------------------------------------------------+
	
    @Override
    public void onCreate(Bundle icicle) {
    	super.onCreate(icicle);
        PREFS_NAME = getString(R.string.p_name).toString();
    	PREFS_THEME = getString(R.string.p_theme).toString();
    	PREFS_PUBLICMAP = getString(R.string.p_public).toString();
    	
    	loadPreferences(); //loadprefs goto's main screen on finish
    	}
    
    @Override
    public void onStop() {
    	super.onStop();
    	finish();
    }
    
    @Override
    public void onRestart() {
    	super.onRestart();
    	loadPreferences();
    }
    
	public void loadPreferences() {
    	//TODO: Keep adding to this!
		//TODO: Make the preferences universal...  If you load preferences here, all the other sub-activities should load as well...  ~_~
    	//**Note** Temporary fix: classes each re-load preferences when called.
		
        final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        if(settings.getBoolean(PREFS_THEME, false)) setTheme(android.R.style.Theme_Dark);
        else setTheme(android.R.style.Theme_White);
        //TODO: Change to custom colors eventually, for now just 2 themes (black-on-white, white-on-black)
        //TODO: Load publicmap preference
        
        splashScreen();
	}
    
    public void splashScreen () {
    	setContentView(R.layout.splash);
    	
    	Runnable finishedtimer = new Runnable() {
 		   public void run() {
 			   gotoMainScreen();
 		   }
 		};
    	
        mHandler.postDelayed(finishedtimer, 500); //500 is just a short time for now, so the splash doesn't last forever
    }
    
    public void gotoMainScreen() {
    	setContentView(R.layout.main); //TODO: Make a nice splash screen
        
        Button m_resume = (Button) findViewById(R.id.m_resume);
        m_resume.setOnClickListener(new Button.OnClickListener() {
        	public void onClick(View v) {
        		resumeGame();
        	}
        });
        Button m_listscripts = (Button) findViewById(R.id.m_listscripts);
        m_listscripts.setOnClickListener(new Button.OnClickListener() {
        	public void onClick(View v) {
        		listScripts();
        	}
        });
        Button m_options = (Button) findViewById(R.id.m_options);
        m_options.setOnClickListener(new Button.OnClickListener() {
        	public void onClick(View v) {
        		gotoOptions();
        	}
        });
	}
    
	public void restartWilfred()
	{	
		setContentView(R.layout.restart);
		
		Button b_yes = (Button) findViewById(R.id.b_yes);
        b_yes.setOnClickListener(new Button.OnClickListener() {
        	public void onClick(View v) {
        		onStop();
        	}
        });
        
    	Button b_later = (Button) findViewById(R.id.b_later);
        b_later.setOnClickListener(new Button.OnClickListener() { 
        	public void onClick(View v) {
        		gotoMainScreen();
        	}
        });
	}
    
	//-------------------------------------------------------+
    // Activities
	//-------------------------------------------------------+
    
    public void listScripts() {
    	gotoMainScreen();  //TODO: Implement
    }
    
    public void resumeGame () {
    	gotoMainScreen();  //TODO: Implement
    }
    
    public void gotoOptions() {
        Intent i = new Intent(this, Options.class);  //TODO: Make intent-filter to find class in manifest
        startSubActivity(i, ACTIVITY_OPTIONS);
    }
    
    //TODO: onActivityResult does not seem to work...  Not sure why.
    @Override
    protected void onActivityResult(int requestCode, int resultCode, String data, Bundle extras) {
    	super.onActivityResult(requestCode, resultCode, data, extras);
    	if (requestCode == ACTIVITY_OPTIONS) {
	    	if (resultCode == RESULT_OK) restartWilfred();
	    	else if (resultCode == RESULT_CANCELED) gotoMainScreen();
    	}
    }
    
	//-------------------------------------------------------+
    // MENU
	//-------------------------------------------------------+

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, RESUMEGAME, R.string.m_resume); //TODO: Show resume if game is underway, else start new game
        menu.add(0, OPTIONS, R.string.m_options);
        menu.add(0, LISTSCRIPTS, R.string.m_listscripts);
        return true;
    }
    
    @Override
    public boolean onMenuItemSelected(int featureId, Item item) {
        super.onMenuItemSelected(featureId, item);
        switch(item.getId()) {
	        case RESUMEGAME:
	            resumeGame();
	            break;
	        case OPTIONS:
	            gotoOptions();
	            break;
	        case LISTSCRIPTS:
	        	listScripts();
        }       
        return true;
    }
    
}